package app.widget;
/**
 * @author EX-XIAOFANQING001
 * @date 2012-3-1
 * @version [Android PABank C01, @2012-3-1]
 * @description
 */

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.longevityassistant.activity.R;

public class ScrollerNavigateView extends FrameLayout {

    private ThumbContainer thumbContainer;

    private boolean isLayout = false;

    private Handler handler = new Handler();

    private ImageView thumb;

    private int lastSelectPosition = -1;

    private boolean isIndicater = false;

    private OnMenuItemSelectListener onMenuItemSelectListener;

    private int thumbWidth;

    private Drawable thumbDrawable;
    
    private LinearLayout llMenusContainer;
    private LinearLayout llThumbContainer;

    public ScrollerNavigateView(Context context) {
        super(context);
        init();
    }

    public ScrollerNavigateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        int thumbRes = attrs.getAttributeResourceValue(null, "thumb", 0);
        thumbDrawable = getContext().getResources().getDrawable(thumbRes);
        init();
    }

    private void init() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.app_widget_scoller_navigate, null);
        root.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        addView(root);
        
        llMenusContainer = (LinearLayout) findViewById(R.id.ll_menus_container);
        llThumbContainer = (LinearLayout) findViewById(R.id.ll_thumb_container);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int childCount = llMenusContainer.getChildCount();
        if (childCount > 0) {
            int sumWidth = 0;
            for (int i = 0; i < childCount; i++) {
                View child = llMenusContainer.getChildAt(0);
                sumWidth += child.getWidth();
            }
            
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)llThumbContainer.getLayoutParams();
            params.width = sumWidth;
            params.setMargins(0, 0, 0, 10);
            llThumbContainer.setLayoutParams(params);
            
            thumbWidth = sumWidth / childCount;
        }

        handler.post(new Runnable() {
            public void run() {
                if (!isLayout) {
                    /*Thumb Container*/
                    if (getChildCount() > 1) {
                        removeViewAt(1);
                    }
                    
                    thumbContainer = new ThumbContainer(getContext());
                    
                    llThumbContainer.addView(thumbContainer, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                    requestLayout();
                    isLayout = true;
                }
            }
        });
    }
    
    public void setMenus(List<Menu> menus) {
        
        int layout = -1;
        Log.i("zbkc", "size():" + menus.size());
        if (menus.size() <= 4) {
            layout = R.layout.app_widget_scoller_navigate2;
        } else {
            layout = R.layout.app_widget_scoller_navigate;
        }
        
        removeAllViews();
        
        View root = LayoutInflater.from(getContext()).inflate(layout, null);
        root.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        addView(root);
        
        requestLayout();
        
        llMenusContainer = (LinearLayout) findViewById(R.id.ll_menus_container);
        llThumbContainer = (LinearLayout) findViewById(R.id.ll_thumb_container);
        
        for (int i =0; i<menus.size(); i++) {
            addMenu(menus.get(i), i);
        }
    }

    private void addMenu(Menu menu, final int position) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.app_widget_navigate_scroller_menu_item, null);
        
        ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        ivIcon.setBackgroundResource(menu.icon);
        ivIcon.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                select(position, false);
            }
        });
        
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvTitle.setText(menu.title);
        
        TextView btn = new TextView(getContext());
        btn.setBackgroundResource(menu.icon);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                select(position, false);
            }
        });
        btn.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
        btn.setText(menu.title);
        btn.setTextSize(11);
        btn.setTextColor(Color.WHITE);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        btn.setLayoutParams(lp);
        
        llMenusContainer.addView(view);
    }

    public void setOnMenuItemSelectListener(OnMenuItemSelectListener onMenuItemSelectListener) {
        this.onMenuItemSelectListener = onMenuItemSelectListener;
    }

    public void select(final int position, boolean isIndicater) {
        this.isIndicater = isIndicater;
        handler.postDelayed(new Runnable() {
            public void run() {
                if (thumbContainer != null) {
                    thumbContainer.select(position);
                }
            }
        }, 500);

    }

    public void hideThumb() {
        if (thumb != null) {
            if (thumb.getVisibility() != View.INVISIBLE) {
                thumb.setVisibility(View.INVISIBLE);
                lastSelectPosition = -1;
            }
        }
    }
    
    public void showThumb() {
        if (thumb != null) {
            if (thumb.getVisibility() != View.VISIBLE) {
                thumb.setVisibility(View.VISIBLE);
            }
        }
    }

    public int getMenuCount() {
        return llMenusContainer.getChildCount();
    }

    public void setThumb(Drawable drawable) {
        thumbDrawable = drawable;
        thumb.setBackgroundDrawable(thumbDrawable);
    }

    public void setThumb(int resId) {
        setThumb(getContext().getResources().getDrawable(resId));
    }

    public interface OnMenuItemSelectListener {
        public void OnMenuItemSelect(int menuPosition);
    }

    public static class Menu {
        public String title;
        public int icon;
    }

    private class ThumbContainer extends LinearLayout {

        private Scroller mScroller;

        public ThumbContainer(Context context) {
            super(context);
            init();
        }

        public void computeScroll() {
            super.computeScroll();
            if (mScroller.computeScrollOffset()) {
                scrollTo(mScroller.getCurrX(), 0);
                postInvalidate();
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int x = (int) event.getX();

            int selectPosition = -1;

            for (int i = 0 ; i< llMenusContainer.getChildCount(); i++) {
                View child = llMenusContainer.getChildAt(i);
                int left = child.getLeft();
                int right = child.getRight();
                if (x > left && x < right) {
                    selectPosition = i;
                    break;
                }
            }

            isIndicater = false;
            showThumb();
            select(selectPosition);

            return super.onTouchEvent(event);
        }

        private void init() {

            mScroller = new Scroller(getContext());

            thumb = new ImageView(getContext());
            if (thumbDrawable != null) {
                thumb.setBackgroundDrawable(thumbDrawable);
            } else {
                thumb.setBackgroundColor(0x44f0f0f0);
            }
            thumb.setVisibility(View.VISIBLE);

            addView(thumb, new LayoutParams(thumbWidth, LayoutParams.FILL_PARENT));

            invalidate();

            forceLayout();
        }

        void select(int position) {

            if (position >= llMenusContainer.getChildCount()) {
                position--;
            }

            if (position < 0) {
                position = 0;
            }

            int scrollX = getScrollX();
            int dx = 0;

            View child = llMenusContainer.getChildAt(position);
            int left = child.getLeft() + (child.getWidth() - thumbWidth) / 2;
            if (scrollX <= 0) {
                dx = Math.abs(getScrollX()) - left;
            } else {
                dx = -(Math.abs(getScrollX()) - left);
            }

            mScroller.startScroll(getScrollX(), 0, dx, 0, 500);
            invalidate();

            if (position != lastSelectPosition) {
                lastSelectPosition = position;

                if (!isIndicater) {
                    if (onMenuItemSelectListener != null) {
                        final int tempP = position;
                        handler.postDelayed(new Runnable() {
                            public void run() {
                                onMenuItemSelectListener.OnMenuItemSelect(tempP);
                            }
                        }, 400);
                    }
                }
            }
        }
    }
}
